package com.boot.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.boot.common.constant.RedisConstant;
import com.boot.system.entity.SysUser;
import com.boot.system.mapper.UserMapper;
import com.boot.system.service.UserCacheService;
import com.boot.util.RedisUtil;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Set;

@Slf4j
@RequiredArgsConstructor
@Service("userCacheService")
public class UserCacheServiceImpl extends ServiceImpl<UserMapper, SysUser> implements UserCacheService {

    private final RedisUtil redisUtil;

    @Override
    public SysUser getCacheUser(String username) {
        SysUser user;
        String prefix = StrUtil.format(RedisConstant.User.INFO, "*", username);
        //获取真实的缓存Key
        String userKey = null;
        Set<String> userKeys = redisUtil.getKeys(prefix);
        for (String item : userKeys) {
            userKey = item;
        }
        if (userKey != null) {
            //缓存中存在
            user = (SysUser) redisUtil.get(userKey);
        } else {
            //缓存中不存在（去数据库中拿）
            user = this.mapper.selectByUsername(username);
            if (user != null) {
                userKey = StrUtil.format(RedisConstant.User.INFO, user.getId(), user.getUsername());
                //缓存数据
                redisUtil.set(userKey, user);
            }
        }
        return user;
    }

    @Override
    public SysUser getCacheUser(Long userId) {
        SysUser user;
        String prefix = StrUtil.format(RedisConstant.User.INFO, userId, "*");
        //获取真实的缓存Key
        String userKey = null;
        Set<String> userKeys = redisUtil.getKeys(prefix);
        for (String item : userKeys) {
            userKey = item;
        }
        if (userKey != null) {
            //缓存中存在
            user = (SysUser) redisUtil.get(userKey);
        } else {
            //缓存中不存在（去数据库中拿）
            user = getById(userId);
            if (user != null) {
                userKey = StrUtil.format(RedisConstant.User.INFO, user.getId(), user.getUsername());
                //缓存数据
                redisUtil.set(userKey, user);
            }
        }
        return user;
    }

    @Override
    public void updateCacheUser(String username) {
        SysUser sysUser = this.mapper.selectByUsername(username);
        if (sysUser != null) {
            String userKey = StrUtil.format(RedisConstant.User.INFO, sysUser.getId(), sysUser.getUsername());
            redisUtil.set(userKey, sysUser);
        }
    }

    @Override
    public void updateCacheUser(Long userId) {
        SysUser sysUser = getById(userId);
        if (sysUser != null) {
            String userKey = StrUtil.format(RedisConstant.User.INFO, sysUser.getId(), sysUser.getUsername());
            redisUtil.set(userKey, sysUser);
        }
    }

    @Override
    public void deleteCacheUser(String username) {
        String prefix = StrUtil.format(RedisConstant.User.INFO, "*", username);
        //获取真实的缓存Key
        String userKey = null;
        Set<String> userKeys = redisUtil.getKeys(prefix);
        for (String item : userKeys) {
            userKey = item;
        }
        if (userKey != null) {
            redisUtil.delete(userKey);
        }
    }

    @Override
    public void deleteCacheUser(Long userId) {
        String prefix = StrUtil.format(RedisConstant.User.INFO, userId, "*");
        //获取真实的缓存Key
        String userKey = null;
        Set<String> userKeys = redisUtil.getKeys(prefix);
        for (String item : userKeys) {
            userKey = item;
        }
        if (userKey != null) {
            redisUtil.delete(userKey);
        }
    }

}
